package com.yjm.test.Spring5.ch5.introductionIntercepor;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultIntroductionAdvisor;

import com.yjm.test.Spring5.ch5.Contact;

public class IntroductionDemo {

	public static void testDefault() {
		Contact contact1 = new Contact();
		contact1.setName("yjm");
		Contact contact2 = new Contact();
		contact2.setName("yjm");
		
		ProxyFactory pf = new ProxyFactory();
		//强制使用CGLib代理，否则使用JDK代理
		pf.setOptimize(true);
		pf.addAdvisor(new IsModifiedAdvisor());
		//pf.addAdvisor(new DefaultIntroductionAdvisor(new IsModifiedMixin()));
		
		pf.setTarget(contact1);
		Contact proxy1 = (Contact) pf.getProxy();
		IsModified proxyInterface1 = (IsModified) proxy1;
		System.out.println(proxy1);
		System.out.println(proxyInterface1);
		System.out.println("is Contact:" + (proxy1 instanceof Contact));
		System.out.println("is Modified:" + (proxy1 instanceof IsModified));
		System.out.println("proxyInterface1 has modified:" + proxyInterface1.isModified());
		proxy1.setName("yjm");
		System.out.println("proxyInterface1 has modified:" + proxyInterface1.isModified());
		proxy1.setName("pwh");
		System.out.println("proxyInterface1 has modified:" + proxyInterface1.isModified());
		
		pf.setTarget(contact2);
		Contact proxy2 = (Contact) pf.getProxy();
		IsModified proxyInterface2 = (IsModified) proxy2;
		System.out.println(proxy1);
		System.out.println(proxyInterface1);
		System.out.println(proxy2);
		System.out.println(proxyInterface2);
		System.out.println("proxyInterface2 has modified:" + proxyInterface2.isModified());
		proxy2.setName("pwh");
		System.out.println("proxyInterface2 has modified:" + proxyInterface2.isModified());
	}
	
	
	public static void main(String[] args) {
		testDefault();
		System.out.println("\n\n");
	}

}
